package puzzle.projecteuler.p100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Problem062 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Map<Long, List<Long>> map = new HashMap<Long, List<Long>>();
		for (long i = 1; i < 100000; i ++) {
			long c = reOrder(i*i*i);
			List<Long> as = map.get(c);
			if (as == null) {
				as = new ArrayList<Long>();
				as.add(i);
				map.put(c, as);
			} else {
				as.add(i);
				map.put(c, as);
			}
			if (map.get(c).size() == 5) {
				System.out.println(map.get(c));
				Long t = map.get(c).get(0);
				System.out.println(t*t*t);
				break;
			}
		}
	}
	
	public static Long reOrder(Long a) {
		List<Integer> as = new ArrayList<Integer>();
		while (a > 0 ) {
			as.add((int)(a%10));
			a /= 10;
		}
		Integer[] tmp = as.toArray(new Integer[as.size()]);
		Arrays.sort(tmp, new Comparator<Integer>() {

			public int compare(Integer o1, Integer o2) {
				return -o1.compareTo(o2);
			}
			
		});
		Long t = 0L;
		for (int d: tmp) {
			t = t*10 + d;
		}
		return t;
	}
}
